package com.mp.approvalreq.servlet.other.module;

import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.entity.ContentType;
import com.mp.approvalreq.entity.DaysOffTimeInfo;
import com.mp.approvalreq.entity.Template;
import com.mp.approvalreq.entity.TemplateComponent;
import com.mp.approvalreq.entity.dto.RequestInfoDTO;
import com.mp.approvalreq.service.CommontApprovalServicePrx;
import com.mp.approvalreq.servlet.AbstractServlet;
import com.mp.approvalreq.util.ApprovalUtils;
import com.mp.approvalreq.util.ServiceUtils;
import com.mp.user.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/getPageWorkDaysOffRequests")
public class GetPageWorkDaysOffRequests extends AbstractServlet {
	
	private static final Logger logger = LoggerFactory.getLogger(GetPageWorkDaysOffRequests.class);
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			User user = getReqUser(req);
			
			Integer currentPage = Integer.valueOf(req.getParameter("currentPage"));
			Integer dataNum = Integer.valueOf(req.getParameter("dataNum"));
			Integer contactId = Integer.valueOf(req.getParameter("contactId"));
			
			CommontApprovalServicePrx commontApprovalService = ServiceUtils.getCommontApprovalServicePrx();
			List<RequestInfoDTO> requestInfoDTOs = commontApprovalService.getPageWorkDaysOffRequests(user.getCompanyId(), contactId, currentPage, dataNum);
			
			wrapResult(requestInfoDTOs);
			
			returnResp(resp, requestInfoDTOs);
		} catch (Throwable e) {
			onThrowable(req, resp, e);
		}
	}
	
	/**
	 * 调流审批列表显示装饰
	 * @param requestInfoDTOs
	 */
	private void wrapResult(List<RequestInfoDTO> requestInfoDTOs) {
		for (RequestInfoDTO requestInfoDTO : requestInfoDTOs) {
			List<TemplateComponent> components = requestInfoDTO.getComponents();
			if (requestInfoDTO.getDefaultType() == Template.DAYS_OFF) {
				for (TemplateComponent component : components) {
					if (component.getType() == ContentType.DAYS_OFF_TOTAL_TIME) {
						DaysOffTimeInfo daysOffTimeInfo = JSONObject.parseObject(component.getValue(), DaysOffTimeInfo.class);
						requestInfoDTO.setSummaryContent(daysOffTimeInfo.getTotalTime());
					}
					
					List<String> times = ApprovalUtils.getTimeList(components, Template.DAYS_OFF);
					requestInfoDTO.setRequestTime(times.get(0));
				}
				
				if (requestInfoDTO.getDefaultType() == Template.WORK_OVERTIME) {
					for (TemplateComponent component : components) {
						if (component.getNum() == 3) {
							requestInfoDTO.setSummaryContent(component.getValue());
						}
					}
				}
			}
		}
	}
	
	private void returnResp(HttpServletResponse resp, List<RequestInfoDTO> requestInfoDTOs) throws IOException {
		JSONObject json = new JSONObject();
		json.put("erpCode", "000");
		json.put("erpMsg", "成功");
		json.put("erpData", requestInfoDTOs);
		
		logger.info(json.toJSONString());
		
		printJson(resp, json.toJSONString());
	}
}
